package com.tosit.bmx.common.shiro;

import com.tosit.bmx.domian.Muser;
import com.tosit.bmx.domian.User;
import com.tosit.bmx.service.admin.MuserService;
import com.tosit.bmx.service.user.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 前台后台验证
 *
 * @author tsl
 * @create 2018-04-20 9:48
 **/
public class AdminVerifyShiro extends AuthorizingRealm {
    @Autowired
    MuserService muserService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        CustomizedToken token = (CustomizedToken)authenticationToken;
        String name = token.getUsername();
        SimpleAuthenticationInfo simpleAuthenticationInfo = null;
        Muser muser = muserService.findsUserByName(name);
        if (muser == null) {
                // 用户名不存在抛出异常
                throw new UnknownAccountException();
        }else {
            simpleAuthenticationInfo= new SimpleAuthenticationInfo(muser,muser.getMpass(),this.getName());
        }
        return simpleAuthenticationInfo;
    }
}
